package com.jinhua.admin.dao.impl;
import com.jinhua.mybatis.util.MybatisPlus;
import com.jinhua.mybatis.util.MybatisPlusBuild;
import com.jinhua.config.IdUtil;
import com.jinhua.admin.dao.AdminUserDao;
import com.jinhua.admin.entity.AdminUserPo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import java.util.List;

/**
* 后台用户的dao层
*
*/

@Repository
public class AdminUserDaoImpl implements AdminUserDao {
    private final int PAGE_SIZE = 10;
    @Override
    public int saveAdminUserIfNotExist(AdminUserPo adminUserPo) {
        return adminUserPo.save();
    }
    @Override
    public int modAdminUser(AdminUserPo adminUserPo) {
        return adminUserPo.update(adminUserPo.build().eq(AdminUserPo::getId, adminUserPo.getId()));
    }
    @Override
    public int delAdminUserByIds(Long... ids) {
        AdminUserPo adminUserPo = new AdminUserPo();
        adminUserPo.setDeletedFlag(Boolean.TRUE);
        return adminUserPo.update(adminUserPo.build().in(AdminUserPo::getId, ids));
    }
    @Override
    public AdminUserPo findAdminUserById(Long id) {
        MybatisPlusBuild build = new MybatisPlusBuild(AdminUserPo.class);
        build.eq(AdminUserPo::getDeletedFlag, Boolean.FALSE);
        build.eq(AdminUserPo::getId, id);
        return MybatisPlus.getInstance().findOne( new AdminUserPo(), build);
    }
    @Override
    public List<AdminUserPo> findAdminUserByAll(AdminUserPo adminUserPo, Integer page, Integer size) {
        MybatisPlusBuild build = new MybatisPlusBuild(AdminUserPo.class);
        build.eq(AdminUserPo::getDeletedFlag, Boolean.FALSE);
        if (adminUserPo.getId() == null) {
            if (StringUtils.isNotEmpty(adminUserPo.getName())) {
                build.like(AdminUserPo::getName,adminUserPo.getName());
            }
            if (adminUserPo.getRoleId()!=null ) {
                build.eq(AdminUserPo::getRoleId,adminUserPo.getRoleId());
            }
            if (adminUserPo.getStatus()!=null ) {
                build.eq(AdminUserPo::getStatus,adminUserPo.getStatus());
            }
            if (StringUtils.isNotEmpty(adminUserPo.getIsAdmin())) {
                build.like(AdminUserPo::getIsAdmin,adminUserPo.getIsAdmin());
            }
            if (adminUserPo.getCreateId()!=null ) {
                build.eq(AdminUserPo::getCreateId,adminUserPo.getCreateId());
            }
            if (StringUtils.isNotEmpty(adminUserPo.getPassword())) {
                build.like(AdminUserPo::getPassword,adminUserPo.getPassword());
            }
            if (StringUtils.isNotEmpty(adminUserPo.getUserName())) {
                build.like(AdminUserPo::getUserName,adminUserPo.getUserName());
            }
            if (StringUtils.isNotEmpty(adminUserPo.getLastLoginIp())) {
                build.like(AdminUserPo::getLastLoginIp,adminUserPo.getLastLoginIp());
            }
            if (page == null) {
                page = 1;
            }
            IdUtil.setTotal(build);
            build.page(page, size==null?PAGE_SIZE:size);
        }
        else {
            build.eq(AdminUserPo::getId, adminUserPo.getId());
        }
        return MybatisPlus.getInstance().findAll( new AdminUserPo()    , build);
    }
}
